Practice Problems and Solutions 2 A

These problems are offered to help understand the lecture material and assist with completing homeworks; they will also form the basis for some problems on the midterms. They are entirely optional, and feel free to skip if you understand a concept!

You should do these problems on paper or mentally first, and only THEN check the solution.

This set of problems uses Bare-Bones Haskell, with explicit data declarations for Pairs and Lists; here is a version of the same problems with the built-in Haskell notations for tuples and lists.

 

2. BareBones Haskell: Types and type checking

Consider the following data declaration:

data D a = A Integer | B Bool | C a 
data Nat = Zero | Succ Nat
data Pair a b = P a b
data List a = Nil | Cons a (List a)
data Tree a = Null | Node (Tree a) a (Tree a)

 

For the first part of these exercises, we will ask you to give the types of various constructors in the above data declarations; then we will define some functions using these data, plus Integer and Bool, and you will need to give the types of the functions, as well as some expressions involving these functions.

We assume that we can have Integer and Bools as defined in the Haskell Prelude. Assume for the purposes of these exercises that all arithmetic operators operate only on Integers, so pretend the type of (+) is:

 (+): Integer -> Integer -> Integer.

If the expression is an error, explain where the error occurs.

Constructors

Give the type of the following constructors.

2.1. Zero

Show Solution

2.2. Succ

Show Solution

2.3. B

Show Solution

2.4. C

Show Solution

2.5. Cons

Show Solution

2.6. P

Show Solution

2.7. Node

Show Solution

Constructor Expressions (with Integer and Bool)

Give the type of the following expressions

2.8. (Succ Zero)

Show Solution

2.9. C 6

Show Solution

2.10. P 5 True

Show Solution

2.12. C (P 4 (A 5))

Show Solution

2.12. C (C (B True))

Show Solution

2.13. P (P 9 (Succ Zero)) (P (C Zero) True)

Show Solution

2.14. (Cons (C 4) (Cons (A 5) Nil))

Show Solution

2.15. (Cons (Cons True Nil) Nil)

Show Solution

2.16. Node Null (Cons (Succ (Succ Zero)) Nil) Null

Show Solution

2.17. C (Node Null (Cons (C (P 3 Zero)) Nil) Null)

Show Solution

Types Involving Constructors and Functions

Suppose for this section that in addition to the data declarations above, we have the following data and function definitions. (The types Either and Maybe are defined in the Prelude and used very often in Haskell programming.)

data Either a b = Left a | Right b
data Maybe a = Nothing | Just a

safeHead Nil        = Nothing
safeHead (Cons x _) = Just x

safeTail  Nil        = Nothing
safeTail (Cons _ xs) = Just xs

fst (P x _) = x
snd (P _ y) = y

f (P x y) = x + y

g (P True x) = Left x
g (P False y) = Right y

h Zero = 0
h (Succ x) = 1 + (h x)
For the next seven problems, just give the types of the functions just defined. The last four will be simple polymorphic types.

2.18. f

Show Solution

2.19. g

Show Solution

2.20. h:

Show Solution

2.21. safeHead

Show Solution

2.22. safeTail

Show Solution

2.23. fst

Show Solution

2.24. snd

Show Solution

Give the types of the following expressions. If the expression can not be typed, explain why.

2.25. g (P (not True) (f (P 4 5)))

Show Solution

2.26. fst (P (h Zero) (g (P True True))))

Show Solution

2.27. safeHead (Cons (fst (P 3 True)) (Cons (h Zero) Nil))

Show Solution

2.28. safeHead (safeTail (Cons (fst (P 3 True)) (Cons (h Zero) Nil)))

Show Solution

2.29.

 g (P (fst x) (snd x))
      where x = (P (snd (P 3 True)) (P False 5))   

Show Solution

2.30.

 g (P (snd x) (fst x))
      where x = (P (snd (P 3 True)) (P False 5))   

Show Solution

2.31.

 g (P (fst x) (fst x))
      where x = (P (snd (P 3 True)) (P False 5))   

Show Solution